package cn.mes.config;

import lombok.Data;

import java.io.Serializable;

// 默认code 1=失败、0=成功，这里不建议像这样写死状态码，应该用枚举维护。
@Data
public class Message<T> implements Serializable {

    private int code = 0;
    private String message;
    private String token;
    private String username;
    private T data;

    public Message() {
    }

    public Message(int code) {
        this.code = code;
    }

    public Message(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Message(int code, String message, String username, String token) {
        this.code = code;
        this.message = message;
        this.username = username;
        this.token = token;
    }

    public Message(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> Message<T> success(T data) {
        Message Message = new Message();
        Message.setData(data);
        return Message;
    }

    public static <T> Message<T> success(String message) {
        Message Message = new Message(0, message);
        return Message;
    }

    public static <T> Message<T> error(int code, String message) {
        Message Message = new Message(code, message);
        return Message;
    }

    public static <T> Message<T> error(String message) {
        Message Message = new Message(1, message);
        return Message;
    }

    //登录返回token
    public static <T> Message<T> login(int code, String message, String username, String token) {
        Message Message = new Message(code, message, username, token);
        return Message;
    }

}
